A struct is a fixed-shape bundle of values, and it compiles today. A constructor is meant to build one automatically - that part still does not. See what to use today.
The struct literal on this page builds into a real game. The constructor further down still does not. Both type-check - the checker has agreed a tagged struct literal and a constructor are well-formed for a while now - but only the struct literal has a real C++ type behind it. This page explains both designs anyway, because the constructor one is real and worth understanding even though it is not ready to use; What is unavailable has the short version of both, and what to write today instead of a constructor specifically.
/// @type {struct Vec2}
var origin = { x: 0, y: 0 };Writing a struct literal like { x: 0, y: 0 } creates a value with exactly those fields, of exactly the types their own values give them. origin.x and origin.y read and write those fields directly. A struct's shape is fixed the moment it is created - you declare every member up front; there is no adding a field to a struct later, for the same reason a variable cannot change type (Variables, types and scope): a struct compiles to a real, fixed C++ struct, and nothing about that shape can move once something else is relying on it.
The example above compiles: origin becomes a real, named C++ type (built once, the first time GBML sees a { x: 0, y: 0 }-shaped value anywhere in the project - a second struct with the exact same fields elsewhere, tagged or not, reuses that same type rather than inventing a second one), and origin.x/origin.y are real member reads. This works the same way for a local var, for an instance variable (tagged or not), and for a list/array/grid of structs - list<struct UndoMove> really does hold real struct values now, the motivating case in the next section. A struct can hold another struct as a field too.
The one place this is not the whole story yet is a script parameter: /// @arg {struct Vec2} m compiles the parameter itself to the right type (as long as "Vec2" names exactly one real shape somewhere else in the project - the tag is the only place a parameter's own shape can come from, since it has no literal of its own to read fields from), but reading one of m's fields inside that function does not check yet - a narrower, separate gap in how a script's own parameter types get inferred, not in struct codegen itself. A function that only passes a struct parameter along without reading its fields is unaffected.
function Vec2(_x, _y) constructor {
x = _x;
y = _y;
static length = function() {
return sqrt(x * x + y * y);
};
}
var a = new Vec2(3, 4);
var d = a.length(); // 5constructor marks a function as building a struct rather than returning an ordinary value: calling it with new is meant to run the function body once, with every bare-name assignment inside (x = _x;) becoming a field of the resulting struct. A static member declared inside a constructor - length above - would be shared by every struct that constructor builds, the right place for a method that only reads self's own fields and does not need a fresh copy per instance.
None of that happens yet. Vec2 compiles to an ordinary function that returns nothing, the same as if constructor were never written - so new Vec2(3, 4) has no value to hand back, and var a = ... has nothing to give a the type of. A static member whose value is a function, like length above, does not compile at all yet either. Both are real, separate gaps in the compiler, not one - see What is unavailable for what to write instead of a constructor today.
An instance variable is good for one value an object needs to remember. A struct is meant for a handful of values that only make sense together - a two-number position, a min/max range, one row of a small lookup table - especially when you want to pass that whole bundle to a function in one go, or keep several of them together rather than as separate parallel arrays. A struct is a value type: assigning one would copy its fields, the same as an int does, not share a reference to the same data.
That is the design, and list<struct UndoMove> builds: one real struct per row, kept in one real container, instead of several parallel ones. templates/sokoban's undo history is made exactly that way - ONE list<struct UndoMove>, one row per move, and each row holds the five values that belong to that move (where the player was, whether a box moved, which one, and where that box was). Every move that changes something pushes one struct, and undo pops the same one back off, so a row always describes one complete move and there is no second list that could drift out of step with it. For a NEW piece of code, reach for the same shape: it is less code than one list per field, and there are no parallel lists to keep in sync.
A struct, by design, has no events, no position, no sprite, and the engine would never spawn or destroy one for you - it is a plain bundle of data and, optionally (once a constructor compiles - still not today), the functions that operate on it. An object (Objects and instances) is the thing with a lifetime the engine manages, events the engine calls, and a place in a room. Both work today, for different jobs: reach for an object when something needs its own place in the world and its own events; reach for a struct when you just need a handful of values that belong together, as a local, an instance variable, or an element of a list/array/grid.